package com.maduar.batch.quartz;

import lombok.SneakyThrows;
import lombok.extern.log4j.Log4j2;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.EnableRetry;
import org.springframework.retry.annotation.Retryable;

import java.time.LocalDateTime;
import java.util.concurrent.TimeUnit;

/**
 * Description:
 *
 * @Author maduar maduar@163.com
 * @Date 2021-07-11 22:29
 */
@Log4j2
@EnableRetry
public class Hello2Job implements Job {

    private int _counter;

    @SneakyThrows
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        log.info("count: {}", _counter);
        log.info("正在抓取job: {}", context);

        boolean sign = true;
        int a = 0;

        test();

        log.info("job execution success [{}]", a);
    }

    @Retryable(value = RuntimeException.class, maxAttempts = 3, backoff = @Backoff(delay = 3000L))
    private void test() {
        _counter++;
        log.info(LocalDateTime.now().toString());
        if (_counter == 5) {
            log.info(LocalDateTime.now().toString());
        } else {
            log.info("count: {}", _counter);
            throw new RuntimeException("aaa");
        }
    }
}
